package com.xingongchang.baolebei.activity;

import org.json.JSONException;
import org.json.JSONObject;

import android.app.ProgressDialog;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.res.Resources;
import android.graphics.Color;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RatingBar;
import android.widget.TextView;
import android.widget.Toast;

import com.external.androidquery.callback.AjaxStatus;
import com.insthub.BeeFramework.activity.BaseActivity;
import com.insthub.BeeFramework.model.BusinessResponse;
import com.insthub.BeeFramework.view.ToastView;
import com.xingongchang.baolebei.R;
import com.xingongchang.baolebei.model.OrderModel;
import com.xingongchang.baolebei.model.ProtocolConst;
import com.xingongchang.baolebei.model.UserInfoModel;

public class ShippingStatusActivity extends BaseActivity implements BusinessResponse {
	
	private TextView title,top_right_text;
	private ImageView back;
//	private TextView name;
//	private TextView order_sn;
//	private LinearLayout info;
//	
//	private String order_id;
//	private OrderModel orderModel;
	private LinearLayout top_right_button;
	private EditText comment_text;
//	private RatingBar ratingBar;
	private UserInfoModel mUserInfoModel;
	private ProgressDialog pd = null;
	private Boolean boolean1=false;
	private SharedPreferences shared;
	private SharedPreferences.Editor editor;
	String uid;
	int comment_id;
	@Override
	protected void onCreate(Bundle savedInstanceState) {		
		super.onCreate(savedInstanceState);
		setContentView(R.layout.shipping_status_list);
		mUserInfoModel=new UserInfoModel(this);
		mUserInfoModel.addResponseListener(this);
		title = (TextView) findViewById(R.id.top_view_text);
		title.setText("商品评价");
		back = (ImageView) findViewById(R.id.top_view_back);
		shared = getSharedPreferences("userInfo", 0);
		back.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {				
				finish();
			}
		});
		top_right_button=(LinearLayout)findViewById(R.id.top_right_button);
		top_right_text=(TextView) findViewById(R.id.top_right_text);
		top_right_text.setText("评价");
		comment_text=(EditText) findViewById(R.id.comment_text);
	/*	ratingBar=(RatingBar) findViewById(R.id.comment_start);*/
		top_right_button.setVisibility(View.VISIBLE);

		// 输入框限制输入字数
		comment_text.addTextChangedListener(new TextWatcher() {
			private CharSequence temp;
			private int selectionStart;
			private int selectionEnd;

			@Override
			public void beforeTextChanged(CharSequence s, int arg1, int arg2,
					int arg3) {
				temp = s;
			}

			@Override
			public void onTextChanged(CharSequence s, int arg1, int arg2,
					int arg3) {
			}

			@Override
			public void afterTextChanged(Editable s) {
				selectionStart = comment_text.getSelectionStart();
				selectionEnd = comment_text.getSelectionEnd();
				if (temp.length() > 120 && temp.length() <5) {
					Toast.makeText(ShippingStatusActivity.this, "文字不能超出120字最少不能少于5字",
							Toast.LENGTH_SHORT).show();
					s.delete(selectionStart - 1, selectionEnd);
					int tempSelection = selectionStart;
					comment_text.setText(s);
					comment_text.setSelection(tempSelection);
				}
			}
		});
		top_right_button.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View arg0) {
				uid = shared.getString("uid", "");
				comment_id=getIntent().getIntExtra("commented",0);
				int id=getIntent().getIntExtra("good_id",0);
				int order_id=getIntent().getIntExtra("order_id",0);
				String comment=comment_text.getText().toString().trim();
				if (uid.equals("")) {
					Intent intent = new Intent(ShippingStatusActivity.this,
							SigninActivity.class);
					startActivity(intent);
					overridePendingTransition(R.anim.push_buttom_in,
							R.anim.push_buttom_out);
				} else {
					if (comment.equals("")) {
						Toast.makeText(ShippingStatusActivity.this, "评论内容不能为空",
								Toast.LENGTH_SHORT).show();
						return;
					}
				mUserInfoModel.getCounment(id,3,comment,order_id);
				}
			}
		});
		
	}
	
	@Override
	public void OnMessageResponse(String url, JSONObject jo, AjaxStatus status)
			throws JSONException {			
		if(url.endsWith(ProtocolConst.criticism)){
			if(mUserInfoModel.responseStatus.succeed==1){
			  	ToastView toast = new ToastView(ShippingStatusActivity.this,"评论成功");
		        toast.setGravity(Gravity.CENTER, 0, 0);
		        toast.show();
		        Intent intent=new Intent(ShippingStatusActivity.this,HistoryActivity.class);
		        intent.putExtra("commented", comment_id);
		        setResult(0, intent);
		        finish();
			}else{
				String errorDesc=jo.optString("error_desc");
				if(errorDesc!=null ||  "".equals(errorDesc)){
					Toast.makeText(ShippingStatusActivity.this, ""+errorDesc, 0).show();
					finish();
				}
			}
		}
	}
}
